package cn.com.pet.administrator.action.encyclopedia;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;

import cn.com.pet.common.base.BaseAction;
import cn.com.pet.common.entity.Pet;
import cn.com.pet.common.service.PetService;
import cn.com.pet.common.util.GuidUtil;
import cn.com.pet.common.util.StringUtil;

@Namespace("/action")
@ParentPackage("base")
@SuppressWarnings({ "serial", "unused" })
public class PetAction extends BaseAction {

	@Autowired
	PetService petService;

	@Action(value = "pageAddPet", results = {
			@Result(name = SUCCESS, location = "/html/administrator/encyclopedia/addPet.jsp") })
	public String pageAddPet() {
		return SUCCESS;
	}

	@Action(value = "pageUpdatePet", results = {
			@Result(name = SUCCESS, location = "/html/administrator/encyclopedia/updatePet.jsp") })
	public String pageUpdatePet() {
		String petId = this.getRequest().getParameter("id");
		this.getRequest().setAttribute("id", petId);
		return SUCCESS;
	}

	@Action(value = "addPet", results = {
			@Result(name = SUCCESS, location = "/html/administrator/encyclopedia/petList.jsp") })
	public String addPet() {

		Pet p = new Pet();
		p.setCreateDate(new Date());
		p.setPetDescription(this.getRequest().getParameter("description"));
		//p.setPetId(GuidUtil.createNewGuid(GuidUtil.FormatString));
		p.setPetName(this.getRequest().getParameter("name"));
		p.setStatus(1);
		p.setPetHot(0);
		p.setPetTypeId(this.getRequest().getParameter("type"));
		p.setUpdateDate(new Date());

		int result = petService.addPet(p);
		return SUCCESS;
	}

	@Action(value = "deletePet", results = {
			@Result(name = SUCCESS, location = "/html/administrator/encyclopedia/petList.jsp") })
	public String deletePet() {

		Pet p = new Pet();

		p.setPetId(StringUtil.parsLong(this.getRequest().getParameter("id")));

		int result = petService.deletePet(p);

		return SUCCESS;
	}

	@Action(value = "updatePet", results = {
			@Result(name = SUCCESS, location = "/html/administrator/encyclopedia/petList.jsp") })
	public String updatePet() {

		Pet p = new Pet();
		p.setPetDescription(this.getRequest().getParameter("description"));
		p.setPetId(StringUtil.parsLong(this.getRequest().getParameter("id")));
		p.setPetName(this.getRequest().getParameter("name"));
		p.setStatus(StringUtil.parsInt(this.getRequest().getParameter("status")));
		p.setPetTypeId(this.getRequest().getParameter("type"));
		p.setUpdateDate(new Date());

		int result = petService.updatePet(p);

		return SUCCESS;
	}

	@Action(value = "findAllPet", results = {
			@Result(name = SUCCESS, location = "/html/administrator/encyclopedia/petList.jsp") })
	public String findAllPet() {

		Map<String, String> parameter = new HashMap<String, String>();

		List<Pet> petList = petService.findAllPet(parameter);
		this.getRequest().setAttribute("allPet", petList);

		return SUCCESS;
	}

	@Action(value = "findPetById", results = {
			@Result(name = SUCCESS, location = "/html/administrator/encyclopedia/petDetail.jsp") })
	public String findPetById() {

		Pet p = new Pet();

		p.setPetId(StringUtil.parsLong(this.getRequest().getParameter("id")));

		Pet pet = petService.findPetById(p);

		this.getRequest().setAttribute("pet", pet);

		return SUCCESS;
	}
}
